<?php
namespace Home\Controller;
use Think\Controller;
header("content-type:text/html;charset=utf-8");
class UserController extends Controller { 
    public function Verify(){
    	$Verify = new \Think\Verify();
    	$Verify->fontSize = 30;
		$Verify->length   = 3;
		$Verify->entry();
    }
    private function thumb($imgurl,$thumburl,$size){
        $image = new \Think\Image(); 
        $image->open($imgurl);
        $image->thumb($size,$size,1)->save($thumburl);
    }
    public function checkName($uname){
        $uname=I('get.uname');
        $res=M('users')->where(array('uname'=>$uname))->find();
        $re=array();
        if(!$res){
            $re['msg']='用户名可用';
            $re['status']=true;     
        }else{
            $re['msg']='用户名已存在!';
            $re['status']=false;    
        }
        $this->ajaxReturn($re);

    }

    public function checkpwd(){
        $pwd=I('get.pwd');
        $uname=I('get.uname');
        $pwd=md5($pwd);
        $res=M('users')->where(array('uname'=>$uname,'pwd'=>"$pwd"))->find();
        $re=array();
        if($res){
            $re['msg']='密码正确!';
            $re['status']=true;     
        }else{
            $re['msg']='密码不正确,请重新输入!';
            $re['status']=false;    
        }
        $this->ajaxReturn($re);

    }
    public function checkcode(){
        $Verify = new \Think\Verify();
        $re=array();
        if($Verify->check(I('get.code'))){
            $re['msg']='验证正确!';
            $re['status']=true;     
        }else{
            $re['msg']='验证码不正确!';
            $re['status']=false;    
        }
        $this->ajaxReturn($re);
    }
  
    public function register(){
    	$this->display();
    }
    public function doRegister(){
        $act=md5(time().rand(1111,9999));
    	$upload = new \Think\Upload();
	    $upload->maxSize   = 10240000 ;
	    $upload->exts      = array('jpg', 'gif', 'png', 'jpeg');
	    $upload->rootPath  = './Uploads/'; 
	    $upload->savePath  = ''; 

	    $info   =   $upload->upload();
	    if(!$info) {
	        $this->error($upload->getError(),U('register'),1);
	        exit;
	    }else{
	       	$photo=$info['photo']['savepath'].$info['photo']['savename'];
            $photo1=$upload->rootPath.$info['photo']['savepath'].'upload_1_'.$info['photo']['savename'];
            $this->thumb($upload->rootPath.$photo,$photo1,100);
            $photo2=$upload->rootPath.$info['photo']['savepath'].'upload_2_'.$info['photo']['savename'];
            $this->thumb($upload->rootPath.$photo,$photo2,50);
        }
        
    	$user=D('users');
    	$_POST['photo']=$upload->rootPath.$photo;
        $_POST['photo1']=$photo1;
        $_POST['photo2']=$photo2;
        $_POST['act']=$act;
    	if($user->create($_POST)){
    		$user->add();
            mymail(I('post.email'),'欣才商城','欣才商城注册激活码','http://127.0.0.1'.U('jh',array('act'=>$act)));

    		$this->success('注册成功,请及时前往邮箱完成注册码激活!',U('Index/index'),1);
    	}else{
    		$this->error($user->getError(),U('register'),1);
    		exit;
    	}
    }
    public function login(){
        if(session('?uname')){
            $this->error('您已经登录',U('Index/index'),1);
        }
        $this->display();
    }
    public function doLogin(){
        $uname=I('post.uname');
        $pwd=md5(I('post.pwd'));
        $res=M('users')->where(array('uname'=>$uname,'pwd'=>$pwd))->find();
        if($res){
            if($res['status']!=2){
               $this->error('您的账号尚未激活,请及时前往邮箱完成注册码激活',U('Index/index'),1); 
               exit;
            }
            session('uname',$uname);
            session('uid',$uid);
            session('userInfo',$res);
            $this->success('登录成功',U('Index/index'),1);
        }else{
            $this->error('登录失败',U('login'),1);
        }
    }
    public function logout(){
        session('[destroy]');
        $this->success('注销成功',U('Index/index'),1);
    }
    public function jh(){
        $act=I('get.act');
        $_POST['status']=2;
        $res=M('users')->where(array('act'=>$act))->find();

        if($res['status']==2){
            $this->error('此账号已经激活,请勿重复!',U('login'),1);
            exit;
        }
        if($res){
            M('users')->where(array('act'=>$act))->save($_POST);
            $this->success('账号激活成功',U('Index/index'),1);                
        }else{
            $this->error('账号激活失败',U('Index/index'),1);
        }
        $this->assign('act',$act);
        $this->display();
    }
}


   
	
	
